package com.cupk.controller;

import com.cupk.entity.Movie;
import com.cupk.repository.MovieRepository;
import com.cupk.service.MovieService;
import com.cupk.util.StringTools;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * @author LY
 * @version 1.0
 */
@WebServlet("/Movie")
public class MovieServlet extends HttpServlet {
    private MovieService movieService = new MovieService();

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String method = req.getParameter("method");
        if(method.equals("findById")){
            String idString = req.getParameter("id");
            int id = Integer.parseInt(idString);
            Movie movie = movieService.findById(id);
            String[] comments = null;
            if(movie.getComment()!=null){
                comments = StringTools.func3(movie.getComment());
            }
            req.setAttribute("comments",comments);
            req.setAttribute("movie",movie);
            req.getRequestDispatcher("result.jsp").forward(req,resp);
        }else if(method.equals("findByArea")){
            List<Movie> list = null;
            String movieName = req.getParameter("movieName");
            list = movieService.findByArea(movieName);
            req.setAttribute("List",list);
            req.getRequestDispatcher("resultList.jsp").forward(req,resp);
        }
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("UTF-8");
        String movieName = req.getParameter("name");    //获取搜索文本
        List<Movie> list = null;
        if("喜剧,爱情,悬疑,动作,科幻,动画".indexOf(movieName) != -1){
            list = movieService.findByType(movieName,false);
        }else if("欧美,法国,英国,意大利,爱尔兰,美国,德国,西班牙,中国,韩国,日本,印度,泰国,台湾".indexOf(movieName) != -1){
            list = movieService.findByArea(movieName);
        }else {
            list = movieService.findMovies(movieName);
        }
        req.setAttribute("List",list);
        req.getRequestDispatcher("resultList.jsp").forward(req,resp);
    }
}
